<template>
    <div class="CarUser-container content-wrapper">
        <div class="filter-container">
            <el-form :model="listQuery" :inline="true" ref="filterForm" style="display: inline-block">

                <el-form-item class="filter-item">
                    {{$route.params.carnum}}
                </el-form-item>

                <el-form-item prop="isHarm" class="filter-item">
                    <el-select v-model="listQuery.isHarm" clearable placeholder="条件" style="width: 150px">
                        <el-option
                                v-for="item in filterOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-button class="filter-item" type="primary" icon="search" @click="handleFilter">搜索</el-button>
                <el-button class="filter-item" type="primary" icon="mars-reset" @click="handleResetFilter">重置
                </el-button>

            </el-form>
        </div>

        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
                  style="width: 100%">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>

            <el-table-column label="车牌号码" align="center">
                <template slot-scope="scope"> {{scope.row.carFullNum}} </template>
            </el-table-column>
            <el-table-column label="记录类型" align="center">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.isHarm | isHarmTagFilter">{{scope.row.isHarm | isHarmFilter(scope.row.isHarm)}}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="处理前" width="250" align="left">
                <template slot-scope="scope">
                    <div style="width: 300px;padding: 10px; cursor: pointer" v-if="scope.row.isHarm == 1" @click="handleShowDetail(scope.row)">
                        <template v-if="scope.row.beforeImageUrl">
                            <img :src="scope.row.beforeImageUrl" style="width: 200px; height: 100px;">
                            <p>备注信息：{{scope.row.beforeDesc!=="" ? scope.row.beforeDesc : ""}}</p>
                            <small>上传人：{{scope.row.beforeUploadUser!=="" ? scope.row.beforeUploadUser : ""}}</small><br>
                            <small>上传时间：{{scope.row.beforeTime!=="" ? scope.row.beforeTime : ""}}</small>
                        </template>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="处理后" width="250" align="left">
                <template slot-scope="scope">
                    <div style="width: 300px;padding: 10px; cursor: pointer" v-if="scope.row.isHarm == 1" @click="handleShowDetail(scope.row)">
                        <template v-if="scope.row.afterImageUrl">
                            <img :src="scope.row.afterImageUrl" style="width: 200px; height: 100px;">
                            <p>备注信息：{{scope.row.afterDesc!=="" ? scope.row.afterDesc : ""}}</p>
                            <small>上传人：{{scope.row.afterUploadUser!=="" ? scope.row.afterUploadUser : ""}}</small><br>
                            <small>上传时间：{{scope.row.afterTime!=="" ? scope.row.afterTime : ""}}</small>
                        </template>

                    </div>
                </template>
            </el-table-column>
            <el-table-column label="赔偿情况" align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.compensate === undefined">未处理</span>
                    <span v-else-if="scope.row.compensate === 1">有赔偿</span>
                    <span v-else="">无赔偿</span>
                    <br>
                    <span v-if="scope.row.compensateDesc === '' || scope.row.compensateDesc === undefined">无备注</span>
                    <span v-else="">备注：{{scope.row.compensateDesc}}</span>
                </template>
            </el-table-column>
            <el-table-column label="赔偿金额" width="130px" align="center">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.compensateMoney && scope.row.compensateMoney != 0">￥{{scope.row.compensateMoney}}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作用户" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.compensateUserName}} </template>
            </el-table-column>
            <el-table-column label="跟进状态" align="center">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.follow | followTagFilter">{{scope.row.follow | followFilter(scope.row.follow, scope.row.isHarm)}}</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="创建时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="250">
                <template slot-scope="scope">
                    <el-button size="small" type="primary" icon="edit" @click="handleCompensate(scope.row)">追讨赔偿</el-button>
                    <el-button size="small" type="primary" icon="edit" @click="handleChangeFollow(scope.row)">已跟进</el-button>
                </template>
            </el-table-column>

        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog title="追讨赔偿" :visible.sync="dialogFormVisible" size="tiny" @close="handleCloseDialog">
            <el-form :model="form" ref="form" :rules="rules" labelPosition="right" labelWidth="80px" style="width: 500px">

                <el-form-item label="状态">
                    <el-radio-group v-model="form.compensate">
                        <el-radio :label="1">有赔偿</el-radio>
                        <el-radio :label="2">无赔偿</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="赔偿金额" prop="compensateMoney">
                    <el-input v-model:number="form.compensateMoney" style="width: 120px;" placeholder="金额：50.00"></el-input>
                    <span style="color:red">注：需补两位小数点，如：50.00</span>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input
                            type="textarea"
                            :autosize="{ minRows: 2, maxRows: 4}"
                            placeholder="请输入内容"
                            v-model="form.compensateDesc">
                    </el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveCompensate">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="详情" :visible.sync="dialogDetailVisible" @close="handleCloseDialog" width="800">
            <el-form :model="form" ref="form" labelPosition="right" labelWidth="90px" style="width: 500px">
                <el-form-item label="上传时间">{{form.beforeTime}}</el-form-item>
                <el-form-item label="备注信息">{{form.beforeDesc}}</el-form-item>
                <el-form-item label="处理前图片">
                    <a :href="form.beforeImageUrl" target="_blank">
                        <img :src="form.beforeImageUrl" style="max-width: 800px; max-height: 400px;" />
                    </a>
                </el-form-item>

                <el-form-item label="上传时间">{{form.afterTime}}</el-form-item>
                <el-form-item label="备注信息">{{form.afterDesc}}</el-form-item>
                <el-form-item label="处理后图片">
                    <a :href="form.afterImageUrl" target="_blank">
                        <img :src="form.afterImageUrl" style="max-width: 800px; max-height: 400px;" />
                    </a>
                </el-form-item>
            </el-form>
        </el-dialog>

    </div>
</template>

<script>
    import { created, deleted, updated, fetchObject, fetchList, saveCompensate, changeFollow } from '@/api/tukuan/state'

    const filterOptions = [{label: '新伤', key: '1'}, {label: '无伤', key: '2'}]


    export default {
        name: 'CarUser',
        data() {
            const checkMoney = (rule, value, callback) => {
                if (value) {
                    if (/^(([1-9]\d*)|\d)(\.\d{1,1})?$/.test(value)) {
                        return callback(new Error('请输入正确的金额数值'));
                    } else {
                        callback()
                    }
                }
                callback()
            }

            return {
                list: null,
                total: null,
                listLoading: true,
                listQuery: {
                    page: 1,
                    rows: 10,
                    carId: this.$route.params.carId,
                    carFullNum: undefined,
                    isHarm: this.$route.params.isHarm,
                    follow: 1,
                    orderBy: 'create_time desc'
                },
                filterOptions,
                dialogFormVisible: false,
                dialogDetailVisible: false,
                form: {
                    id: null,
                    compensate: 1,
                    compensateDesc: '',
                    compensateMoney: ''
                },
                rules: {
                    compensateMoney: [
                        { validator: checkMoney, trigger: 'blur' },
                    ],
                }
            }
        },
        created() {
            this.getList()
        },
        filters: {
            isHarmFilter(status) {
                const statusArray = ['新伤', '无伤']
                return statusArray[status?--status:'']
            },
            isHarmTagFilter(status) {
                const statusArray = ['danger', 'success']
                return statusArray[status?--status:'']
            },
            followFilter(status, isHarm) {
                if(status === undefined) {
                    return '未处理'
                } else {
                    if(status === 1) {
                        return '未跟进'
                    } else {
                        return '已跟进'
                    }
                }
            },
            followTagFilter(status) {
                if(status === undefined) {
                    return '未处理'
                } else {
                    if(status === 1) {
                        return 'danger'
                    } else {
                        return 'success'
                    }
                }
            }
        },
        methods: {
            getList() {
                this.listLoading = true
                fetchList(this.listQuery).then(response => {
                    this.list = response.data.list
                    this.total = response.data.total
                    this.listLoading = false
                }).catch((error)=>{
                    console.log(error)
                    this.listLoading = false
                })
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleCloseDialog() {
                this.resetForm()
            },
            handleFilter() {
                this.listQuery.page = 1
                this.getList()
            },
            handleResetFilter() {
                this.$refs.filterForm.resetFields()
                this.getList()
            },
            handleCompensate(row) {
                fetchObject(row.id).then(response => {
                    this.dialogFormVisible = true
                    this.form = Object.assign({}, response.data)
                })
            },
            handleShowDetail(row) {
                fetchObject(row.id).then(response => {
                    this.dialogDetailVisible = true
                    this.form = Object.assign({}, response.data)
                })
            },
            handleChangeFollow(row) {
                this.$confirm('确认更改为已跟进？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.form.id = row.id
                    changeFollow(this.form).then(response => {
                        if(response.data.status) {
                            this.$notify({
                                title: '成功', message: response.data.message,
                                type: 'success', duration: 2000
                            })
                            this.getList()
                        } else {
                            this.$notify({
                                title: '发生错误', message: response.data.message,
                                type: 'error', duration: 2000
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            saveCompensate() {
                this.$refs['form'].validate((valid) => {
                    if(valid) {
                        saveCompensate(this.form).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getList()
                            } else {
                                this.$notify({
                                    title: '操作失败', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                            this.dialogFormVisible = false
                        })
                    }
                })

            },
            resetForm() {
                this.$refs['form'].resetFields()
                this.form = {
                    id: null,
                    compensate: 1,
                    compensateDesc: '',
                    compensateMoney: null
                }
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>